package model;

/**
 * Created by Esferat on 02.08.2014.
 */


import com.google.common.io.Files;
import org.openqa.selenium.*;
import org.testng.ITestResult;
import org.testng.Reporter;

import java.io.File;
import java.io.IOException;

import static org.openqa.selenium.Keys.*;
import static org.testng.Assert.assertEquals;
import static org.testng.AssertJUnit.assertTrue;


public class UserHelper extends HelperBase {

    public UserHelper(TestManager manager) {
        super(manager);
    }


    public void fillRegistrationForm() throws InterruptedException {
    }

    public void agreeUserAgreement() {
        click(By.id("agreement"));
    }

    public void saveChangesProfileInfo() {
    }

    public void closeForm() {
        click(By.cssSelector("span.ui-icon.ui-icon-closethick"));
    }

    public void openBaseUrl() {
        openUrl(getBaseUrl());
    }

    public void login(String login, String password) {
        // login only if not logged in yet
        if (isLogout()) {
            click(By.cssSelector(".login"));
            type(By.cssSelector("#username"), login);
            type(By.cssSelector("#password"), password);
            click(By.name("login"));

        }
    }

    public void sendMessage() {
        //click(By.xpath("//button[@type='button']"));
    }

    public void writeMessage(String message) {
        //type(By.id("message"), message);
    }

    public void submit() {
        click(By.name("submit"));
    }

    public void goToCandidatePage(String id) {
        //openUrl(getBaseUrl() + "/people/" + id);
    }

    public void clearField(By locator) {
        findElement((locator)).clear();
    }

    public void sendKeystoField(By locator) {
        findElement((locator)).sendKeys("new task");
    }

    public void sendKeystoField2(By locator) {
        findElement((locator)).sendKeys("exchange");
    }

    public boolean isLogout() {
        return isElementPresent(By.xpath("//*[@class='login']"));
    }

    public boolean isYourProfilePage() {
        return isElementPresent(By.linkText("More Details"));
    }

    public void login() {
        login(manager.getMainLogin(), manager.getMainPassword());
    }

    public void loginWithWrongPass() {
        login(manager.getMainLogin(), "invPass");
    }

    public void openUserMenu() throws InterruptedException {
        //By me = By.cssSelector("#userpic");
        //waitForElementVisible(me);
        //moveTheMouse(findElement(me));
    }

    public void gotoSettingsPage() throws InterruptedException {
        //openUserMenu();
        //click(By.id("settings-page"));
    }

    public void gotoYourProfilePage() throws InterruptedException {
        //openUserMenu();
        //click(By.id("my-profile-page"));
    }

    public void logoutUser() throws InterruptedException {
        click(By.xpath("//*[@class='logout']"));
    }

    public void takeScreenshot(ITestResult result) throws IOException {
        if (!result.isSuccess()) {
            File screenshot1 = new File(result.getMethod().getMethodName() + ".png");
            screenshot1.delete();
            File screenshotTempFile = ((TakesScreenshot) driver)
                    .getScreenshotAs(OutputType.FILE);
            Files.copy(screenshotTempFile, screenshot1);
            Reporter.log("<a href='" + screenshot1.getName() + "'>"
                    + result.getMethod().getMethodName() + " screenshot</a>");
        }
    }

    public void delCookies() throws InterruptedException {
        driver.manage().deleteAllCookies();
        //driver.navigate().refresh();
        Thread.sleep(2000);
    }

}


